#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

int show()
{
  printf("hello show\n");
  exit(0);
}

int main()
{
  pid_t id = fork();
  if(id > 0)
  {
    //father
    int status = 0;
    printf("I am father, pid:%d, ppid:%d\n", getpid(), getppid());
    //pid_t ret = wait(&status);
    pid_t ret = waitpid(id, &status, 0);
    if(ret >= 0)
    {
      printf("wait child successfully, ret:%d\n", ret);
      printf("status:%d\n", status);
      printf("child exit code:%d\n", (status>>8) & 0xff);
      printf("child exit signal:%d\n", status & 0x7f);
    }
    printf("father run......\n");
    sleep(3);
  }
  else if(id < 0)
    perror("fork fail");
  else{
    //child
    int count =  0;
    while(count < 10)
    {    
      printf("I am child, pid:%d, ppid:%d\n", getpid(), getppid());
     // if(count == 10)
     //   count = 1 / 0;
    // if(count == 10)
    // {
    //   int* pa;
    //   *pa = 100;
    // }
      count++;
      sleep(1);
    }
    exit(100);
  }

  //printf("hello Linux");
  //exit(10);
  //_exit(11);


  //show();
  //printf("hello Linux\n");
  //for(int i = -1; i < 135; i++)
  //printf("return_value[%d]:%s\n", i, strerror(i));
  //sleep(1);
  return 0;
}
